from etgui import *
from PageLayout import PotTextField
from ModelDictEditor import ModelDictEditor

"""
	SetValue("PSEUDO.Defaults.PATH",path)
	SetValue("PSEUDO.CopyForEditing.PATH",1)
 
	GetValue("PSEUDO.Level.PATH") # 0 means at edit level
"""
class LayeredModelDictEditor(ModelDictEditor):
	"""A class that does simple list editing"""
 
	def SetDefaults(self,path):
		self.SetPSEUDOValue(action="Defaults",value=1)
		self.Reload()
	def _MakePseudoPath(self,action,path=None,i=None):
		result="PSEUDO."+action
		if i is not None and path is not None:
			raise RuntimeError,"i and path used!"
		if path is not None:
			npath=path
		elif i is not None:
			npath=self.PathAt(i)
		else:
			npath=self.slotpath
		return "PSEUDO."+action+"."+npath
 
	def SetPSEUDOValue(self,action,value=None,path=None,i=None):
		path=self._MakePseudoPath(action,path,i)
		self.model.SetValue(path,value)
	def GetPSEUDOValue(self,action,path=None,i=None):
		path=self._MakePseudoPath(action,path,i)
		return self.model.GetValue(path)
	def IsPSEUDOValueChangable(self,action,path=None,i=None):
		path=self._MakePseudoPath(action,path,i)
		return self.model.IsValueChangable(path)
 
	def IsAtEditLevel(self,path):
		return self.GetPSEUDOValue(action="Level",path=path)==0
 
	def CanDelete(self,i):
		path=self.PathAt(i)
		return self.model.IsValueChangable(self.path)\
				and self.IsAtEditLevel(path)
	def DoDelete(self,i):
		self.model.RemoveAt(self.PathAt(i))
		self.Reload()
		self.Select(self.GetSelected())
		return 0 # do not modify the list!
 
	CanRename=CanDelete
	def DoRename(self,i):
		key_value=ModelDictEditor.DoRename(self,i)
		if key_value:
			key,value=key_value
			self.model.RemoveAt(self.PathAt(i))
			self.model.SetValue(self.MakePath(key),value)
			self.Reload(key)
		return None # do not modify the list!
	def CanCopy(self,i):
		return self.model.IsValueChangable(self.path)
	def DoCopy(self,i):
		key_value=ModelDictEditor.DoCopy(self,i)
		if key_value:
			key,value=key_value
			self.model.SetValue(self.MakePath(key),value)
			self.Reload(key)
		return None # do not modify the list!
 
	def DoInsert(self,i,v):
		ModelDictEditor.DoInsert(self,i,v)
		self.Reload()
		return None
 
	def CanNew(self,i):
		return self.model.IsValueChangable(self.path)
	def DoNew(self,i):
		key_value=ModelDictEditor.DoNew(self,i)
		if key_value:
			key,value=key_value
			self.model.SetValue(self.MakePath(key),value)
			self.Reload(key)
		return None
	def CopyFrom(self):
		self.SetPSEUDOValue(action="CopyForEditing",value=1)
		self.Reload()
	def CanCopyFrom(self,i):
		return self.IsPSEUDOValueChangable(action="CopyForEditing",i=i)
 
	label_copy_for_editing="Set Writable"
	def MakeCopyFromButton(self):
		return self.NewButton(self.label_copy_for_editing,self.CopyFrom,self.CanCopyFrom)
	def GetActionButtonConstructors(self):
		return [self.MakeCopyFromButton]
 
	def DoMakeItem(self,key_value):
		try:
			key,value=key_value
		except (TypeError,ValueError):
			key=key_value
		if not self.IsAtEditLevel(self.MakePath(key)):
			font=gItalicFont
		else:
			font=gSysFont
		return TextItem(str(key),font=font)
 
